package com.wx.springboot.step7.service.impl;

import com.wx.springboot.step7.entity.Permission;
import com.wx.springboot.step7.repository.PermissionRepository;
import com.wx.springboot.step7.service.PermissionService;
import com.wx.springboot.step7.vo.PermissionVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionRepository permissionRepository;

    @Override
    public void streamTest() {
        List<Permission> permissionList = permissionRepository.findAll();
        
        //filter操作：获取权限类型为目录的权限
        List<Permission> dirList = permissionList.stream()
                .filter(permission -> permission.getType() == 0)
                .collect(Collectors.toList());
        log.info("filter操作：{}",dirList);
        
        //map操作：获取所有权限的id
        List<Long> idList = permissionList.stream()
                .map(permission -> permission.getId())
                .collect(Collectors.toList());
        log.info("map操作：{}",idList);
        
        //limit操作：获取前5个权限
        List<Permission> firstFiveList = permissionList.stream()
                .limit(5)
                .collect(Collectors.toList());
        log.info("limit操作：{}",firstFiveList);
        
        //count操作：获取所有权限目录权限的个数
        long dirPermissionCount = permissionList.stream()
                .filter(permission -> permission.getType() == 0)
                .count();
        log.info("count操作：{}",dirPermissionCount);
        
        //sorted操作：将所有权限按先目录后菜单再按钮的顺序排序
        List<Permission> sortedList = permissionList.stream()
//                .sorted((permission1,permission2)->{return permission1.getType().compareTo(permission2.getType());})
                .sorted(Comparator.comparing(Permission::getType))
                .collect(Collectors.toList());
        log.info("sorted操作：{}",sortedList);
        
        //skip操作：跳过前5个元素，返回后面的
        List<Permission> skipList = permissionList.stream()
                .skip(5)
                .collect(Collectors.toList());
        log.info("skip操作：{}",skipList);
        
        //collect转map操作：将权限列表以id为key，以权限对象为值转换成map
        Map<Long, Permission> permissionMap = permissionList.stream()
                .collect(Collectors.toMap(permission -> permission.getId(), permission -> permission));
        log.info("collect转map操作：{}",permissionMap);
        
        //forEach操作，对集合中的元素进行迭代
        permissionList.stream().forEach(permission-> log.info("forEach操作{}",permission));
    }

    @Override
    public List<PermissionVo> treeList() {
        List<Permission> permissionList = permissionRepository.findAll();
        List<PermissionVo> result = permissionList.stream()
                .filter(permission -> permission.getPid().equals(0L))
                .map(permission -> covert(permission, permissionList))
                .collect(Collectors.toList());
        return result;
    }

    /**
     * 将权限转换为带有子级的权限对象
     * 当找不到子级权限的时候map操作不会再递归调用covert
     */
    private PermissionVo covert(Permission permission, List<Permission> permissionList) {
        PermissionVo vo = new PermissionVo();
        BeanUtils.copyProperties(permission, vo);
        List<PermissionVo> children = permissionList.stream()
                .filter(subPermission -> subPermission.getPid().equals(permission.getId()))
                .map(subPermission -> covert(subPermission, permissionList))
                .collect(Collectors.toList());
        vo.setChildren(children);
        return vo;
    }
}
